package com.cuit.controller;

import com.cuit.dao.impl.OrderDaoImpl;
import com.cuit.pojo.Order;
import com.cuit.pojo.Patient;
import com.cuit.service.Impl.OrderServiceImpl;
import com.cuit.service.OrderService;
//import sun.plugin.com.Dispatcher;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/acceptOrder")
public class AcceptOrder extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession se= req.getSession();
        String d_id = req.getParameter("d_id");
        String d_day = req.getParameter("d_week");//坐诊的星期几
        String d_name = req.getParameter("d_name");
        String time = req.getParameter("sitting_time");//坐诊的时间上午、下午、晚上

        Patient patient = (Patient)se.getAttribute("patient");
        OrderServiceImpl service = new OrderServiceImpl();
        int day = Integer.parseInt(d_day);
        int max_Order = service.queryByIDFromDOrder(d_id, day);
        int num = service.queryByIDFromOrder2(d_id, day);
        Order exits = service.queryOneOrder(patient.getP_id(), d_id, day, time);

        if(num<max_Order&&exits==null){
            service.addInfoToOrder(new Order(patient.getP_id(), patient.getP_name(), d_id, d_name, time, day));
            if(num==-1){
                service.saveNewInfoToOrder2(d_id,day);
            }else{
                service.saveInfoToOrder2(d_id, day);
            }
            Order order = service.queryOneOrder(patient.getP_id(), d_id, day, time);
            se.setAttribute("nowOrder",order);
            resp.sendRedirect(req.getContextPath()+"/pages/order/showThisOrder.jsp");
        }else{
            resp.sendRedirect(req.getContextPath()+"/pages/order/orderFalse.jsp");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
